
/*
 * Copyright © 2021 https://www.cestc.cn/ All rights reserved.
 */

package com.zx.learn.flink.web;

import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.source.SourceFunction;

import java.text.SimpleDateFormat;

/**
 * <dependency>
 *     <groupId>org.apache.flink</groupId>
 *     <artifactId>flink-runtime-web_2.12</artifactId>
 *     <version>${flink.version}</version>
 * </dependency>
 *
 * @desc: 在windos本地创建Flink的含有WebUI的执行环境
 */
public class FlinkWebUi {

    public static void main(String[] args) throws Exception {

        // 使用流的执行环境类创建本地并含有WebUI的执行环境
        StreamExecutionEnvironment env =
                StreamExecutionEnvironment.createLocalEnvironmentWithWebUI(new Configuration());

        env
                .addSource(new SourceFunction<String>() {

                    boolean flag = true;
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

                    @Override
                    public void run(SourceContext<String> sourceContext) throws Exception {
                        while (flag) {
                            sourceContext.collect(sdf.format(System.currentTimeMillis()));
                            Thread.sleep(1000);
                        }
                    }

                    @Override
                    public void cancel() {
                        flag = false;
                    }
                })
                .print("WebUIForIDEA >>>>>");

        // 启动执行
        env.execute("WebUIForIDEA");
    }
}
